package com.gpframework.module.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.brand.domain.BrandStoreUserRela;
import com.gpframework.module.brand.domain.req.BrandStoreUserRelaReq;
import com.gpframework.module.module.domain.BrandStore;
import com.gpframework.module.module.domain.req.BrandStoreReq;
import com.gpframework.module.module.domain.req.BrandStoreUpdate;
import com.gpframework.module.module.domain.vo.BrandStoreVo;
import com.gpframework.module.module.service.IBrandStoreService;
import com.gpframework.module.pay.utils.joinpay.sdk.utitls.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName BrandStoreController
 * @Author gpframework
 * @Date 2022-03-25
 * @Version 1.0.0
 * @Description 门店Controller
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/module/brandStore")
public class BrandStoreController extends BaseController {

    private final IBrandStoreService iBrandStoreService;
    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2022-03-25
     * @Param brandStore
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:brandStore:list')")
    @GetMapping("/list")
    public TableDataInfo<BrandStoreVo> list(BrandStoreReq brandStoreReq) {
        startPage();
        QueryWrapper<BrandStore> lqw = new QueryWrapper<BrandStore>();
        brandStoreReq.generatorQuery(lqw, true);
        List<BrandStoreVo> list = iBrandStoreService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * 用户门店列表
     *
     * @param storeUserRelaReq
     * @return
     */
    @ApiOperation("用户门店列表")
    @RequestMapping(value = "/userStoreList", method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('module:brandStore:list')")
    public AjaxResult<BrandStoreVo> userStoreList(BrandStoreUserRelaReq storeUserRelaReq) {
        if (StringUtils.isEmpty(storeUserRelaReq.getUserId())) {
            storeUserRelaReq.setUserId(storeUserRelaReq.getLoginWxUserId());
        }
        QueryWrapper<BrandStoreUserRela> lqw = new QueryWrapper<>();
        storeUserRelaReq.generatorQuery(lqw, true);
        lqw.groupBy("t1.store_id");
        List<BrandStoreVo> list = iBrandStoreService.userStoreList(lqw);
        return AjaxResult.success(list);
    }

    /**
     * 修改门店信息
     *
     * @param brandStoreUpdate
     * @return
     */
    @PutMapping
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('brand:brandStore:edit')")
    @Log(title = "修改门店", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Valid @RequestBody BrandStoreUpdate brandStoreUpdate) {
        return toAjax(iBrandStoreService.edit(brandStoreUpdate) ? 1 : 0);
    }

    /**
     * 修改门店状态与区域地址
     *
     * @param brandStoreUpdate
     * @return
     */
    @PutMapping(value = "/updateStoreInfo")
    @ApiOperation("修改门店状态与区域地址")
    @Log(title = "修改门店状态与区域地址", businessType = BusinessType.UPDATE)
    public AjaxResult updateStoreInfo(@Valid @RequestBody BrandStoreUpdate brandStoreUpdate) {
        if (StringUtil.isEmpty(brandStoreUpdate.getStoreId())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iBrandStoreService.updateStoreInfo(brandStoreUpdate) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2022-03-25
     * @Param brandStore
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:brandStore:export')")
    @Log(title = "导出门店", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BrandStoreReq brandStoreReq) {
        QueryWrapper<BrandStore> lqw = new QueryWrapper<BrandStore>();
        brandStoreReq.generatorQuery(lqw, true);
        List<BrandStoreVo> list = iBrandStoreService.listVo(lqw);
        ExcelUtil<BrandStoreVo> util = new ExcelUtil<BrandStoreVo>(BrandStoreVo.class);
        return util.exportExcel(list, "brandStore");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2022-03-25
     * @Param brandStore
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('brand:brandStore:query')")
    @GetMapping(value = "/{storeId}")
    public AjaxResult<BrandStoreVo> getInfo(@PathVariable("storeId") Long storeId) {
        return AjaxResult.success(iBrandStoreService.getBrandStore(storeId));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2022-03-25
     * @Param brandStore
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:brandStore:remove')")
    @Log(title = "删除门店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds) {
        return toAjax(iBrandStoreService.removeByIds(Arrays.asList(storeIds)) ? 1 : 0);
    }


}
